<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <!-- 一级页面 -->
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <!-- 二级页面 -->
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <!-- 详情页 -->
        <XtxBreadItem> {{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <GoodsImage :pics="goods.mainPictures" />
          <!-- 商品售卖信息 -->
          <GoodsSales :goods="goods" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品名字价格 -->
          <GoodsName :goods="goods" />
          <!-- 商品sku -->
          <GoodsSku :goods="goods" @change="getSku" />
          <!-- 加减组件 -->
          <!-- v-model写道自定义组件身上原理 ： 1.子传父 -->
          <XtxNumbox v-model="buyNum" :max="goods.inventory" />
          <!-- 按钮组件 -->
          <XtxButton @click="addCart" style="margin-top: 20px"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot v-for="item in 3" :key="item" :type="item" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 热榜
import GoodsHot from './componebts/goods-hot.vue'
// 商品标签页
import GoodsTabs from './componebts/goods-tabs.vue'
// 商品名字价格
import GoodsName from './componebts/goods-name.vue'
// 商品售卖信息
import GoodsSales from './componebts/goods-sales.vue'
// 导入图片展示
import GoodsImage from './componebts/goods-image.vue'
// 导入面包屑接口
import { findGoods } from '../../api/goods.js'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
// 提示信息
import Message from '@/components/Message/index.js'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsName,
    GoodsSales,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    const route = useRoute()
    const goods = ref({})
    // 购买数量
    const buyNum = ref(1)
    // 商品详情
    provide('goods', goods)
    async function getGoods () {
      const { data } = await findGoods(route.params.id)
      goods.value = data.result
      // console.log(goods.value, 8888888)
    }
    // sku 当前数据   信息: 库存+价格
    let skuGood = null
    const getSku = (sku) => {
      console.log(sku, '我是sku')
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
        // 存储sku
        skuGood = sku
      } else {
        skuGood = null
      }
    }
    // 加入购物车
    /**
     * 思路 :
     * 1.有效的sku才能加入购物车
     */
    const store = useStore()
    const addCart = () => {
      if (!skuGood) return Message({ type: 'error', text: '请选择有效规则' })
      // 库存数量
      if (skuGood.inventory === 0) return Message({ type: 'error', text: '无货' })
      // 加入到购物车  存储到vuex中
      console.log('存储到vuex')
      store.dispatch('cart/addCartAction', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: skuGood.skuId, // 有效商品skuId
        price: skuGood.oldPrice,
        nowPrice: skuGood.price,
        attrsText: skuGood.specsText, // 属性
        stock: skuGood.inventory, // 库存
        selected: true, // 购物车状态
        isEffective: true, // 是否是有效商品
        count: buyNum.value // 商品数量
      }).then(() => {
        Message({ type: 'success', text: '加入购物车成功!' })
      })
    }
    onMounted(() => {
      getGoods()
    })
    return { goods, getSku, buyNum, addCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
